<template>
  <div id="app">
    <el-card shadow="always">
      <!--条件查询-->
      <el-form :inline="true" size="medium" :model="specsSearch" class="demo-form-inline">
        <el-form-item >
          <el-input v-model="specsSearch.spec_name" placeholder="规格名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="search" icon="el-icon-user">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" plain @click="editSpecs" icon="el-icon-plus">添加</el-button>
        </el-form-item>
      </el-form>

      <el-table
          :data="specsList"
          style="width: 100%">
        <el-table-column
            label="规格ID"
            prop="id"
            align="center"
            width="180">
        </el-table-column>
        <el-table-column
            label="规格名称"
            align="center"
            prop="spec_name">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页导航-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>
    <!--新增规格对话框-->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      <el-form :model="specs" style="float: left">
        <el-form-item label="规格名称" :label-width="formLabelWidth">
          <el-input v-model="specs.spec_name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveSpecs">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import specsApi from "@/api/specs"
import userApi from "../../vue-ssm/src/api/user";
export default {
  data() {
    return {
      specsList: [],      // 当前页的所有用户信息集合
      page:1,               // 当前页
      pageSize:5,           // 每页大小
      total:100,            // 总记录数
      searchSpecs:{},     // 要查找的用户信息对象
      specsSearch: {},    // 代表查询条件对象
      dialogFormVisible:false,  // 控制对话框的显示隐藏
      formLabelWidth:'100px',   // 对话框提示文本的宽度
      specs: {},        //用户对象
      title: '添加规格',   //对话框标题
    }
  },
  created() {
    // 1. 进行分页查询
    // this.findByPage(this.page,this.pageSize)
    this.search()
  },

  methods: {
    // 1. 查询所有用户
    findSpecs(){
      this.$axios.get('/specs/list.do')
          .then(res => {
            // this.userList = res.data;
            console.log("数据：",res.data)
            this.specsList = res.data
          })
    },
    // 2. 进行分页查询
    async findByPage(){
      // 若依框架写法：
      const {data:res} = await spcesApi.findByPage(this.page,this.pageSize)
      this.specsList = res.rows
      this.total = res.total
    },
    // 4. 条件查询带分页
    async search(){
      const {data} = await specsApi.search(this.page,this.pageSize,this.specsSearch)
      this.specsList = data.rows
      this.total = data.total
    },
    // 5. 编辑规格
    editSpecs(){
      this.dialogFormVisible = true
    },
    // 6. 保存规格
    async saveSpecs(){
      const {data:res} = await specsApi.save(this.specs)
      if (res.code === 200){
        this.$message({
          type: 'success',
          message: res.message
        })
        // 关闭对话框
        this.dialogFormVisible = false
        // 重新刷新页面
        this.search()
      }else {
        this.$message({
          type: 'error',
          message: res.message
        })
      }
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    // 每页大小改变时事件
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.search()
    },
    // 当前页改变时事件
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      this.search()
    }
  }
}
</script>

<style>
.el-card{
  width: 90%;
  margin: 0 auto;
}
.el-form{
  float: right;
}
.el-pagination{
  text-align: right;
  margin-top: 15px;
}
#app{
  display: flex;
  justify-content: center;
}
</style>
